# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import pandas as pd


class XgkinfoPipeline:
    def open_spider(self, spider):
        # 在这里定义需要的数据 方便我们进行存储
        self.data = []

    def process_item(self, item, spider):
        self.data.append(
            (item.get('id'), item.get('title'), item.get('content'), item.get('imgSrc'),
             item.get('url'), item.get('classes')))
        return item

    def close_spider(self, spider):
        data = pd.DataFrame(self.data, columns=['id', '标题', '内容', '图片路径', 'url', '类别'])
        data.to_excel('codeweb.xlsx')
